package com.paydemo.pay.RabbitMQ;

import com.alibaba.fastjson.JSON;
import com.paydemo.pay.entity.PayOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 支付消息生产者
 */
@Slf4j
@Component
public class PaymentMQProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送订单创建消息
     */
    public void sendOrderCreateMessage(PayOrder payOrder) {
        PaymentMessage message = buildPaymentMessage(payOrder);
        try {
            log.info("发送订单创建消息，订单号：{}", payOrder.getOrderNo());
            rabbitTemplate.convertAndSend(MqConstants.PAYMENT_EXCHANGE, MqConstants.PAYMENT_ORDER_CREATE_KEY, message);
        } catch (Exception e) {
            log.error("发送订单创建消息失败，订单号：{}，错误：{}", payOrder.getOrderNo(), e.getMessage(), e);
        }
    }

    /**
     * 发送支付成功消息
     */
    public void sendPaymentSuccessMessage(PayOrder payOrder) {
        PaymentMessage message = buildPaymentMessage(payOrder);
        try {
            log.info("发送支付成功消息，订单号：{}", payOrder.getOrderNo());
            rabbitTemplate.convertAndSend(MqConstants.PAYMENT_EXCHANGE, MqConstants.PAYMENT_SUCCESS_KEY, message);
        } catch (Exception e) {
            log.error("发送支付成功消息失败，订单号：{}，错误：{}", payOrder.getOrderNo(), e.getMessage(), e);
        }
    }

    /**
     * 发送支付失败消息
     */
    public void sendPaymentFailMessage(PayOrder payOrder) {
        PaymentMessage message = buildPaymentMessage(payOrder);
        try {
            log.info("发送支付失败消息，订单号：{}", payOrder.getOrderNo());
            rabbitTemplate.convertAndSend(MqConstants.PAYMENT_EXCHANGE, MqConstants.PAYMENT_FAIL_KEY, message);
        } catch (Exception e) {
            log.error("发送支付失败消息失败，订单号：{}，错误：{}", payOrder.getOrderNo(), e.getMessage(), e);
        }
    }

    /**
     * 发送支付超时消息
     */
    public void sendPaymentTimeoutMessage(PayOrder payOrder) {
        PaymentMessage message = buildPaymentMessage(payOrder);
        try {
            log.info("发送支付超时消息，订单号：{}", payOrder.getOrderNo());
            rabbitTemplate.convertAndSend(MqConstants.PAYMENT_EXCHANGE, MqConstants.PAYMENT_TIMEOUT_KEY, message);
        } catch (Exception e) {
            log.error("发送支付超时消息失败，订单号：{}，错误：{}", payOrder.getOrderNo(), e.getMessage(), e);
        }
    }

    /**
     * 构建支付消息对象
     */
    private PaymentMessage buildPaymentMessage(PayOrder payOrder) {
        return PaymentMessage.builder()
                .orderNo(payOrder.getOrderNo())
                .title(payOrder.getTitle())
                .amount(payOrder.getAmount())
                .userId(payOrder.getUserId())
                .payStatus(payOrder.getPayStatus())
                .payType(payOrder.getPayType())
                .tradeNo(payOrder.getTradeNo())
                .extraInfo(payOrder.getCallbackContent())
                .sendTime(new Date())
                .build();
    }
} 